package net.easyjava.third.aliyun.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import net.easyjava.tools.date.DateTools;
import net.easyjava.tools.image.ImageTools;
import net.easyjava.tools.image.bean.ImageBean;
import net.easyjava.tools.lang.StringTools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by wxnacy on 16/6/29.
 */
public abstract class BaseOSSClient {
    protected String accessId;
    protected String accessKey;
    protected static final String ROOT_DEFAULT = "common";
    protected static final String SEPARATOR_DEFAULT = "/";
    protected static final String TYPE_LOCAL = "local";//本地
    protected static final String TYPE_WEB = "W";//网络
    protected static final String TYPE_INPUTSTREAM = "I";//流


    /**
     * 获取 OSSClient
     * @param endpoint
     * @return
     */
    public  OSSClient getClient(String endpoint){
        OSSClient client = new OSSClient(endpoint,this.accessId, this.accessKey) ;
        return client;
    }




    /**
     *
     * @param endpoint aliyun oss 必备
     * @param bucket aliyun oss 必备
     * @param accessId aliyun oss 必备
     * @param accessKey aliyun oss 必备
     * @param root 上传文件根目录 不传默认common
     * @param type
     * @param path
     * @param url
     * @param is
     * @return
     * @throws AliyunOSSException
     */
    @Deprecated
    protected String upload(String endpoint,String bucket,String accessId,String accessKey,
                            String root,String type,String path,String url,InputStream is) throws AliyunOSSException, IOException {
        OSSClient client = getClient(endpoint);
        InputStream inputStream = null;
        String fileName = null;
        switch (type){
            case TYPE_LOCAL:
                if(StringTools.isNull(path)){
                    throw new AliyunOSSException("path can't be empty");
                }
                inputStream = new FileInputStream(new File(path));
                fileName = createKey(root,path);
                break;
            case TYPE_WEB:
                if(StringTools.isNull(url)){
                    throw new AliyunOSSException("url can't be empty");
                }
                fileName = createKey(root, url);
                //判断是否为图片地址、如果是图片获取相关信息
                ImageBean imageBean = ImageTools.getImageFromUrl(url);
                inputStream = new URL(url).openStream();
                if(imageBean != null){
                    fileName = fileName.substring(0,fileName.lastIndexOf("."));
                    fileName = fileName + "_"+imageBean.getWidth() + "x" + imageBean.getHeight() + imageBean.getSuffix();
                }


                break;
            case TYPE_INPUTSTREAM:
                if(is == null){
                    throw new AliyunOSSException("is can't be null");
                }
                inputStream = is;

                fileName = createKey(root,path);
                break;
            default:
                throw new AliyunOSSException("type is error");
        }

        client.putObject(bucket, fileName, inputStream);
        // 关闭client
        client.shutdown();
        if(inputStream != null){
            inputStream.close();
        }
        return fileName;
    }


    /**
     * 通过url地址上传文件
     * @param endpoint
     * @param bucket
     * @param url
     * @param key
     * @return
     * @throws IOException
     */
    public String upload(String endpoint, String bucket, URL url,String key) throws IOException {
        URLConnection conn = url.openConnection();
        InputStream inputStream = conn.getInputStream();
        if(StringTools.isEmpty(key)){
            key = createKey(ROOT_DEFAULT,url.getPath());
        }
        return this.upload(endpoint, bucket, inputStream, key);
    }

    /**
     * 通过file上传文件
     * @param endpoint
     * @param bucket
     * @param file
     * @param key
     * @return
     * @throws IOException
     */
    public String upload(String endpoint, String bucket, File file,String key) throws IOException {
        if(!StringTools.isEmpty(key)){
            key = formatKey(key);
        }else{
            key = createKey(ROOT_DEFAULT,file.getAbsolutePath());
        }
        OSSClient client = getClient(endpoint);
        client.putObject(bucket, key, file);
        // 关闭client
        client.shutdown();
        return key;
    }


    /**
     * 通过流上传文件
     * @param endpoint
     * @param bucket
     * @param inputStream
     * @param key
     * @return
     * @throws IOException
     */
    public String upload(String endpoint, String bucket, InputStream inputStream,String key) throws IOException {
        key  = formatKey(key);
        OSSClient client = getClient(endpoint);
        client.putObject(bucket, key, inputStream);
        // 关闭client
        client.shutdown();
        if (inputStream != null) {
            inputStream.close();
        }
        return key;
    }

    protected String formatKey(String key){
        if(key.startsWith("/")){
            key = ROOT_DEFAULT+key;
        }else if(key.startsWith("common/")) {

        }else if(key.indexOf("/")>0){

        }else {
            key = ROOT_DEFAULT+ "/" + key;
        }
        return key;
    }

    protected  String getSuffix(String path){
        String suffix = path.substring(path.lastIndexOf("."));
        return suffix;
    }
    public String createKey(String root ,String path){
        if(StringTools.isNull(root)){
            root = ROOT_DEFAULT;
        }
        String key = root + SEPARATOR_DEFAULT + DateTools.getDateDirString() +System.currentTimeMillis()+getSuffix(path);
        return key;
    }
}
